package com.zhuhjay.chat.config;

import com.zhuhjay.chat.protocol.Serializable;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

/**
 * @author ZhuHJay
 * @date 2023/3/9 17:07
 */
public abstract class Config {
    private static final Properties PROPERTIES;

    static {
        try {
            InputStream in = Config.class.getResourceAsStream("/application.properties");
            PROPERTIES = new Properties();
            PROPERTIES.load(in);
        } catch (IOException e) {
            throw new ExceptionInInitializerError("初始化异常");
        }
    }

    /** 获取主机 **/
    public static String getHost() {
        return PROPERTIES.getProperty("server.host", "localhost");
    }

    /** 获取端口号 **/
    public static int getPort() {
        return Integer.parseInt(PROPERTIES.getProperty("server.port", "8080"));
    }

    /** 获取序列化算法 **/
    public static Serializable.Algorithm getSerializeAlgorithm() {
        String algorithm = PROPERTIES.getProperty("serialize.algorithm", "Java");
        return Serializable.Algorithm.valueOf(algorithm);
    }

    /** 获取服务层映射 **/
    public static Map<String, String> getServiceProperties() {
        HashMap<String, String> map = new HashMap<>(2);
        PROPERTIES.stringPropertyNames()
                .stream().filter(name -> name.endsWith("Service"))
                .forEach(name -> map.put(name, PROPERTIES.getProperty(name)));
        return map;
    }

    /** 是否开启 debug 环境 **/
    public static boolean isDebug() {
        String property = PROPERTIES.getProperty("debug.enable", "false");
        return Boolean.parseBoolean(property);
    }

}
